<?php

Yii::import('application.modules.news.models.News');
Yii::import('application.modules.news.models.NewsCategories');

class NewsController extends Controller {

    /**
     * Lists all models.
     */
    public $layout = '//layouts/column2';

    public function init() {
        Yii::app()->theme = 'home';
    }

    public function actionDetail() {
        $model = $this->loadModel($_GET['id']);
        $criteria = new CDbCriteria(array(
            'condition' => 'id !=' . $_GET['id'],
            'order' => 'id DESC',
        ));
        $criteria->compare('category_id', $model->category->id);
        $criteria->compare('status', 1);
        $criteria->limit = 10;
        $news = News::model()->findAll($criteria);
        $this->render('detail', array('model' => $model, 'news' => $news));
    }

    public function actionView() {
        $model = NewsCategories::model()->findByPk($_GET['id']);
        $criteria = new CDbCriteria(array(
            'order' => 'id DESC',
        ));

            $criteria->compare('category_id', $_GET['id']);
            $criteria->compare('status', 1);
            $count = News::model()->count($criteria);
            $pages = new CPagination($count);
            $pages->pageSize = 12;
            $pages->applyLimit($criteria);
            $news = News::model()->findAll($criteria);
            $this->render('view', array('model' => $model, 'news' => $news, 'pages' => $pages));

    }

    public function loadModel($id) {
        $model = News::model()->findByPk($id);
        if ($model === NULL)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}